// homepages/personalpages/identitypage/identitypage.js
var app = getApp();
var interval = null //倒计时函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fun_id: 2,
    time: '获取验证码', //倒计时 
    currentTime: 61,
    currentTimeseccess: 4,
    src: '/images/info@2x.png', 
    successsrc: '/images/success@2x.png',
    failsrc: '/images/fail@2x.png',
    infomessage:'身份认证时间为周一至周五8点到17点，请在该时间段进行验证',
    showsuccessDialog: false,
    showfailDialog: false,
    successtimer: '',//定时器名字
    countDownNum: '3',//倒计时初始值
    name:'',
    idnum:'',
    banknum:'',
    phonenum:'',
    messagenum:'',
  },
  getNameInputValue: function (e) {
    this.setData({
      name: e.detail.value.replace(/\s+/g, '')
    })

  },
  getIdnumInputValue: function (e) {
    this.setData({
      idnum: e.detail.value.replace(/\s+/g, '')
    })
  },
  getBanknumInputValue: function (e) {
    this.setData({
      banknum: e.detail.value.replace(/\s+/g, '')
    })
  },
  getPhonenumInputValue: function (e) {
    this.setData({
      phonenum: e.detail.value.replace(/\s+/g, '')
    })
  },
  getMessagenumInputValue: function (e) {
    this.setData({
      messagenum: e.detail.value.replace(/\s+/g, '')
    })
  },

  //短信验证码
  getVerificationCode() {
    this.getCode();
    var that = this
    that.setData({
      disabled: true
    })
  },
  //短信验证码倒计时方法
  getCode: function (options) {
    // 获取验证码请求
    this.getphonemessage(); 

    // 倒计时
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
  },
  //确认按钮事件
  btnconm:function(){
    console.log('真或者假：', app.checkIsEmpenty(this.data.name, '姓名'));
    
    //先判断姓名，身份证，银行账号，手机号码，验证码是否为空
    if (app.checkIsEmpenty(this.data.name, '姓名') ||
      app.checkIsEmpenty(this.data.idnum, '身份证') ||
      app.checkIsEmpenty(this.data.banknum, '银行账号') ||
      app.checkIsEmpenty(this.data.phonenum, '手机号码') ||
      app.checkIsEmpenty(this.data.messagenum, '验证码')) {
    } else {
      //确认接口请求
      if (app.checkPhonenum(this.data.phonenum)&&
        app.checkIdnum(this.data.idnum)){
        this.sbmitmessagehttppost();
      }
    }
  },

  //弹出成功对话框
  togglesuccessDialog() {
    this.successcountDown();
    this.setData({
      showsuccessDialog: !this.data.showsuccessDialog
    });

  },

  //弹出失败对话框
  togglefailDialog() {
    this.failcountDown();
    this.setData({
      showfailDialog: !this.data.showfailDialog
    });

  },
  //成功弹窗倒计时
  successcountDown: function () {
    let that = this;
    let countDownNum = that.data.countDownNum;//获取倒计时初始值
    //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
    that.setData({
      timer: setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
        //每隔一秒countDownNum就减一，实现同步
        countDownNum--;
        //然后把countDownNum存进data，好让用户知道时间在倒计着
        that.setData({
          countDownNum: countDownNum
        })
        //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
        if (countDownNum == 0) {
          //这里特别要注意，计时器是始终一直在走的，如果你的时间为0，那么就要关掉定时器！不然相当耗性能
          //因为timer是存在data里面的，所以在关掉时，也要在data里取出后再关闭
          clearInterval(that.data.timer);
          //关闭定时器之后，可作其他处理codes go here
          wx.redirectTo({
          //   url: '/homepages/commonpages/protocolpage/protocolpage'
              url: '/homepages/personalpages/facerecognpage/facerecognpage',
          })
        }
      }, 1000)
    })
  },
  //失败弹窗倒计时
  failcountDown: function () {
    let that = this;
    let countDownNum = that.data.countDownNum;//获取倒计时初始值
    //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
    that.setData({
      timer: setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
        //每隔一秒countDownNum就减一，实现同步
        countDownNum--;
        //然后把countDownNum存进data，好让用户知道时间在倒计着
        that.setData({
          countDownNum: countDownNum
        })
        //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
        if (countDownNum == 0) {
          //这里特别要注意，计时器是始终一直在走的，如果你的时间为0，那么就要关掉定时器！不然相当耗性能
          //因为timer是存在data里面的，所以在关掉时，也要在data里取出后再关闭
          clearInterval(that.data.timer);
          //关闭定时器之后，可作其他处理codes go her
          that.setData({
            showfailDialog: !that.data.showfailDialog,
            countDownNum:'3'
          });
        }
      }, 1000)
    })
  },
  //提交身份认证信息
  sbmitmessagehttppost: function () {
    // this.togglesuccessDialog();
    var that = this;
    app.globalData.AccNo = that.data.banknum;
    let url = app.globalRequestUrl('domainNameB', 'siteC');
    let data = {
      Mkt_ID: app.globalData.Mkt_ID,//市场编号
      // Mkt_ID: "45060435300079",//市场编号
      WeCht_Idr: app.globalData.WeCht_Idr,//微信标识
      // WeCht_Idr: "oyTj25d5gD2doUNEXhC69Og6klwo",//微信标识
      Mrch_Tp: app.globalData.identity,//商家类型
      // Mrch_Tp: '01',//商家类型
      // Mrch_Nm: app.globalData.Mrch_Nm,//商户名称
      Mrch_Nm: that.data.name,//商户名称
      Crdt_No: that.data.idnum,//证件号码
      RcvPymt_AccNo: that.data.banknum,//收款账号
      MblPh_No: that.data.phonenum,//手机号码
      ValiCd: that.data.messagenum,//验证码
    }
    app.appRequest('post', url, data, (res) => {
      console.log(res)
      if('00'==res.Txn_Rslt_Cd){
        that.togglesuccessDialog();
          app.globalData.idnt_Ctfn_St ='01';//01标识身份认证成功
        wx.setStorageSync('AccNo', that.data.banknum)
        
      } else if(res.Txn_Rslt_Cd == "02"){
        wx.redirectTo({
          url: '/typechoosepage/typechoosepage',
        })
      }else{
        that.togglefailDialog();
      }
    }, (err) => {
      console.log('请求错误信息：  ' + err.errMsg);
      app.showErrormessage(err.errMsg);
      that.togglefailDialog();
    });
  },

  //获取短信验证码
  getphonemessage: function () {
    var that=this;
    let url = app.globalRequestUrl('domainNameB', 'siteM');
    let data = {
      mktId: app.globalData.Mkt_ID,//市场编号
      wechtIdr: app.globalData.WeCht_Idr,//微信标识
      phoneNum: that.data.phonenum,//
    }
    app.appRequest('post', url, data, (res) => {
      
       if (res.code == "0"){//失败
        app.showErrormessage("发送短信验证码失败");
      }else if(res.code=="1"){//成功
        app.showErrormessage("成功发送短信验证码");
       } else if (res.code == "2"){
         wx.redirectTo({
           url: '/typechoosepage/typechoosepage',
         })
      }
      var resStr = JSON.stringify(res)
      console.log('短信验证码接口 ' + resStr)
    }, (err) => {
      console.log('请求错误信息：  ' + err.errMsg);
      app.showErrormessage(err.errMsg);
    });
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})